import 'package:flutter/material.dart';

class SwitcherStatic extends StatefulWidget {
  const SwitcherStatic({super.key});

  @override
  State<SwitcherStatic> createState() => _SwitcherStaticState();
}

class _SwitcherStaticState extends State<SwitcherStatic> {
  bool _isFirst = true;
  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: 400,
      decoration: BoxDecoration(
        border: Border.all(color: Colors.black, width: 1),
        borderRadius: BorderRadius.circular(6)
      ),
      child: Column(
        children: [
          ElevatedButton(
            onPressed: () {
              setState(() {
                _isFirst = !_isFirst;
              });
            },
            child: const Text('切换 Switcher')
          ),

          AnimatedSwitcher(
            duration: const Duration(seconds: 1),
            transitionBuilder: (Widget child, Animation<double> animation) {
              return FadeTransition(opacity: animation, child: child);
            },
            child: _isFirst
              ? Container(
                  key: ValueKey(1),
                  width: 100,
                  height: 100,
                  color: Colors.amber[100],
                )
              : Container(
                  key: ValueKey(2),
                  width: 200,
                  height: 200,
                  color: Colors.grey,
                )
          )
        ],
      ),
    );
  }
}